class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]
  before_action :set_user_groups, only: [:new, :new_all, :edit]

  # GET /users
  # GET /users.json
  def index
    @user_count = User.count
    @users = User.all.page(params[:page]).per(100)
  end

  # GET /users/1
  # GET /users/1.json
  def show
  end

  # GET /users/new
  def new
    @user = User.new
  end

  # GET /users/1/edit
  def edit
  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render action: 'show', status: :created, location: @user }
      else
        format.html { render action: 'new' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # GET /users/new_all
  def new_all
    @user_group = UserGroup.new
    @popover = render_to_string partial: 'user_groups/new'
  end

  # POST /users/create_all
  def create_all
    email_valid = params[:email_valid]
    subscribe = params[:subscribe]
    last_sign_in = params[:last_sign_in]
    success = 0
    failures = {}
    params[:users].each_line do |line|
      email, name = line.split(',').map(&:strip).reverse
      name = email.split('@')[0] if name.blank?
      user = User.new(email: email, name: name, email_valid: email_valid, subscribe: subscribe, last_sign_in: last_sign_in)
      if user.save
        success += 1
      else
        failures[line] = user.errors.full_messages
      end
    end
    redirect_to last_url, notice: "#{success} user(s) created.",
                          alert: failures
  end

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url }
      format.json { head :no_content }
    end
  end

  def search
    @users = User.search_by_email_or_domain_name params[:s]
    render partial: 'promotion_mails/search'
  end

  private
    def set_user_groups
      @user_groups = UserGroup.all.order(name: :asc)
    end

    def check_permission!
      not_found unless current_admin.can? :create_user
    end

    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:email, :name, :subscribe, :serial_number, :user_group_id, :last_sign_in, :email_valid)
    end
end
